package com.ruoyi.web.controller.platform;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.MonitorConfig;
import com.ruoyi.system.service.MonitorConfigService;
import com.ruoyi.web.dto.EnabledDto;
import com.ruoyi.web.dto.MonitorConfigAddDto;
import com.ruoyi.web.dto.MonitorConfigUpdateDto;
import com.ruoyi.web.util.BeanTools;
import com.ruoyi.web.util.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/monitor/config")
public class MonitorConfigController extends BaseController {

    @Autowired
    private MonitorConfigService monitorConfigService;

    @PreAuthorize("@ss.hasPermi('config:monitor:list')")
    @GetMapping("/list")
    public AjaxResult list() {
        List<MonitorConfig> list = monitorConfigService.validList();
        return success(list);
    }

    @PreAuthorize("@ss.hasPermi('config:monitor:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody MonitorConfigAddDto dto) {
        ValidatorUtils.validateEntity(dto);
        MonitorConfig config = BeanTools.copy(dto, MonitorConfig.class);
        config.setCreateBy(getUsername());
        monitorConfigService.save(config);
        return success();
    }

    @PreAuthorize("@ss.hasPermi('config:monitor:edit')")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody MonitorConfigUpdateDto dto) {
        ValidatorUtils.validateEntity(dto);
        MonitorConfig config = BeanTools.copy(dto, MonitorConfig.class);
        config.setUpdateBy(getUsername());
        monitorConfigService.updateById(config);
        return success();
    }

    @PreAuthorize("@ss.hasPermi('config:monitor:remove')")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody List<Long> ids) {
        monitorConfigService.delete(ids);
        return success();
    }

    @PreAuthorize("@ss.hasPermi('config:monitor:edit')")
    @PostMapping("/change/enabled")
    public AjaxResult changeEnabled(@Valid @RequestBody EnabledDto dto) {
        monitorConfigService.changeEnabled(dto.getId(), dto.getEnabled());
        return success();
    }
}
